package com.jingdianjichi.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthPermissionDTO;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色DTO
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Resource
    private AuthPermissionDomainService permissionDomainService;

    /**
     * 新增权限
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO permissionDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(permissionDTO));
            }

            //参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(permissionDTO.getName()),"权限名称不能是空的");
            Preconditions.checkNotNull(permissionDTO.getParentId(),"权限父亲id不能是空的");

            AuthPermissionBO permissionBO=AuthPermissionDTOConverter.INSTANCE.ConvertDtoToBO(permissionDTO);

            Boolean result=permissionDomainService.add(permissionBO);

            return Result.ok(result);

        }catch (Exception e){
            log.error("PermissionController.add.error:{}",e.getMessage(),e);

            return Result.fail("权限新增失败");
        }
    }

    /**
     * 修改权限
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO permissionDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(permissionDTO));
            }

            //参数校验
            Preconditions.checkNotNull(permissionDTO.getId(),"权限id不能是空的");

            AuthPermissionBO permissionBO=AuthPermissionDTOConverter.INSTANCE.ConvertDtoToBO(permissionDTO);

            Boolean result=permissionDomainService.update(permissionBO);

            return Result.ok(result);

        }catch (Exception e){
            log.error("PermissionController.update.error:{}",e.getMessage(),e);

            return Result.fail("权限修改失败");
        }
    }

    /**
     * 删除权限
     */
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO permissionDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(permissionDTO));
            }

            //参数校验
            Preconditions.checkNotNull(permissionDTO.getId(),"权限id不能是空的");

            AuthPermissionBO permissionBO=AuthPermissionDTOConverter.INSTANCE.ConvertDtoToBO(permissionDTO);

            Boolean result=permissionDomainService.delete(permissionBO);

            return Result.ok(result);

        }catch (Exception e){
            log.error("PermissionController.delete.error:{}",e.getMessage(),e);

            return Result.fail("权限修改失败");
        }
    }

    /**
     * 用户的权限查询亲
     */
    @RequestMapping("/getPermission")
    public Result<List<String>> getPermission(String userName){
        try {
            log.info("PermissionController.getPermission.dto:{}", JSON.toJSONString(userName));

            //参数校验
            Preconditions.checkNotNull(!StringUtils.isBlank(userName),"用户UserName不能是空的");

            List<String> permissionList=permissionDomainService.getPermission(userName);

            return Result.ok(permissionList);

        }catch (Exception e){
            log.error("PermissionController.getPermission.error:{}",e.getMessage(),e);

            return Result.fail("查询用户权限失败");
        }
    }

}
